/*首字母大写*/
function upperFirstLetter(str)
{   
   return str.replace(/\b\w+\b/g, function(word) {   
   return word.substring(0,1).toUpperCase( ) +  word.substring(1);   
 });   
}

/*首字母小写*/
function lowerFirstLetter(str)
{   
   return str.replace(/\b\w+\b/g, function(word) {   
   return word.substring(0,1).toLowerCase( ) +  word.substring(1);   
 });   
}

/*注册require.config*/
function requireConfig(){
	require.config({
	    paths: {
	        jquery: '../../js/jquery-1.12.3',
	        backbone: '../../bower_components/backbone/backbone',
	        underscore: '../../bower_components/lodash/dist/lodash',
	        localStorage:'../../bower_components/backbone.localStorage/backbone.localStorage',
	        validation:'../../bower_components/backbone.validation/dist/backbone-validation',
	        service:'service',
	        indexModel:'model/indexModel',
	        indexCollection:'model/indexCollection',
	    },
	    shim: {
    	   'underscore': {
                exports: '_'
            },
            'jquery': {
                exports: '$'
            },           
            'backbone': {
                deps: ['underscore', 'jquery'],
                exports: 'Backbone'
            },
            'validation':{
        	deps: ['backbone']          	
            }
	    }
	});
}

/*jquery获取url参数*/
function getUrlParam(name){
	var reg
	 = new RegExp("(^|&)"+
	 name +"=([^&]*)(&|$)");
	var r
	 = window.location.search.substr(1).match(reg);
	if (r!=null) return unescape(r[2]); return null;
}

/*重新组装Paginator数据*/
function getPaginator(data,state){
/*	return {				    
		        per_page: state.pageSize,
		        total_entries: data.length,
		        total_pages: Math.ceil(data.length/state.pageSize),
		        page: state.currentPage
		   };*/
	return {				    
	        ItemsPerPage: state.pageSize,
	        TotalItems: data.length,
	        TotalPages: Math.ceil(data.length/state.pageSize),
	        CurrentPage: state.currentPage
	   };
}

/*返回分页片的起始位置*/
function sliceStart(dataPaginator){
	return dataPaginator.ItemsPerPage*(dataPaginator.CurrentPage-1);
}

/*返回分页片的结束位置*/
function sliceEnd(dataJson,start,dataPaginator){
	return (start+dataPaginator.ItemsPerPage)<dataJson.length?(start+dataPaginator.ItemsPerPage):dataJson.length;
}

/*判断对象在本地localStorage是否存在*/
function isExistInLocalStorage(objKey,store){
	if(localStorage.getItem(store.name+'-'+objKey)!=null){
		return true;
	}else{
		return false;
	}
}

/*根据key获取本地localStorage数据*/
function getDataFormLocalStorage(objKey){
	var store= localStorage.getItem(objKey);
	
	return (store && store.split(",")) || [];
}

/*添加本地localStorage数据项*/
function addItemToLocalStorage(objKey,data,store){
	localStorage.setItem(store.name+'-'+objKey,JSON.stringify(data));
	var records = getDataFormLocalStorage(store.name);
	records.push(objKey);
	localStorage.setItem(store.name,records.join(','));
}

/*更新本地localStorage数据项*/
function updateItemToLocalStorage(objKey,data,store){
	localStorage.setItem(store.name+'-'+objKey,JSON.stringify(data));
	
	var records = getDataFormLocalStorage(store.name);
	
    var modelId = objKey;
    if (!contains(records, modelId)) {
      records.push(modelId);
      localStorage.setItem(store.name,records.join(','));
    }
}

/*删除本地localStorage数据项*/
function deleteItemToLocalStorage(objKey,data,store){

	localStorage.removeItem(store.name+'-'+objKey);
	
	var records = getDataFormLocalStorage(store.name);
	
    var modelId = objKey;
    for (var i = 0, id; i < records.length; i++) {
      if (records[i] === modelId) {
        records.splice(i, 1);
      }
    }
    localStorage.setItem(store.name,records.join(','));
}

/*删除本地localStorage数据*/
function deleteToLocalStorage(store){
	var records = getDataFormLocalStorage(store.name);
	
	for (var i = 0,data=null; i < records.length; i++) {
	    localStorage.removeItem(store.name+'-'+records[i]);
    }
	
	localStorage.removeItem(store.name);
}

/*获取本地localStorage数据项*/
function getItemFormLocalStorage(objKey,store){
	return JSON.parse(localStorage.getItem(store.name+'-'+objKey));
}

/*判断数组是否包含项*/
function contains(array, item) {
  var i = array.length;
  while (i--) if (array[i] === item) return true;
  return false;
}